package dhm.com.dhmshop.view.mine.myshop;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.ShopOrderAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetAd;
import dhm.com.dhmshop.entity.ShopOrderBean;
import dhm.com.dhmshop.utils.SpUtils;

public class ShoporderActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.recy)
    XRecyclerView recy;

    private String uid;
    private String shop_id;
    private PressenterImpl pressenter;
    private int page;
    private ShopOrderAdapter shopOrderAdapter;
    private List<ShopOrderBean.DataBean> allOrder;

    @Override
    protected int getLayout() {
        return R.layout.activity_shoporder;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        page=1;
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(this, "uid");
        shop_id = SpUtils.getString(this, "shop_id");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getOrder();
            }

            @Override
            public void onLoadMore() {
                getOrder();
            }
        });
        getOrder();
    }

    private void getOrder() {

        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("shop_id",shop_id);
        map.put("page",page+"");
        pressenter.sendMessage(this,Constant.AllShopOrders,map, ShopOrderBean.class);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof ShopOrderBean){
            ShopOrderBean shopOrderBean= (ShopOrderBean) data;
            if (shopOrderBean.getCode()==1){
                if (shopOrderBean.getData()!=null&&shopOrderBean.getData().size()>0){
                    if (page==1){
                        allOrder = shopOrderBean.getData();
                        shopOrderAdapter = new ShopOrderAdapter(this);
                        shopOrderAdapter.setaddClick(new ShopOrderAdapter.OnShopClick() {
                            @Override
                            public void item(int posttion) {
                                Intent intent=new Intent(ShoporderActivity.this,ShoporderdActivity.class);
                                intent.putExtra("order_id",allOrder.get(posttion).getOrder_id()+"");
                                intent.putExtra("uid",allOrder.get(posttion).getUid()+"");
                                startActivity(intent);
                            }
                        });
                        recy.setAdapter(shopOrderAdapter);
                    }else {
                        allOrder.addAll(shopOrderBean.getData());
                    }
                    page++;
                    shopOrderAdapter.setShopList(allOrder);

                }
            }else {
                Toast.makeText(this, shopOrderBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
            if (allOrder==null||allOrder.size()==0){
                recy.setVisibility(View.GONE);
            }

            recy.loadMoreComplete();
            recy.refreshComplete();
        }
    }

    @Override
    public void fail(String error) {

    }
}
